package editor
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_hscrollArea;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class pi_previewItem extends uif_ecomponent {
		protected var _prevImage: pi_previewSource;
		protected var _dataItem: *;
		
		public function pi_previewItem(index: int, parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, a_data: *=null, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			scrollArea.items[index].control = this;
			_dataItem = a_data;
			var ir: Rectangle = getInner();
			_prevImage = new pi_previewSource(this, ir.x, ir.y, ir.width, ir.height, 'preview');
			if (_dataItem) {
				this.addEventListener(MouseEvent.CLICK, function(e: MouseEvent): void {
					app.dispatchEvent(new ObjectEvent(Event.SELECT, e.target));
				});
				if (scrollArea.selectItem && scrollArea.style['select'] && checkItemSelect(_dataItem)) {
					scrollArea.selectIndex = index;
				};
			}
		}
		
		protected function checkItemSelect(item: Object): Boolean {
			return scrollArea.selectItem.object.id == item.id;
		}
		
		protected function get scrollArea(): uif_hscrollArea {
			return parent.parent as uif_hscrollArea;
		}
		
		public function get data(): * {
			return _dataItem;
		}
	}
}